<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('分类')" prop="keyReport">
            <el-input v-model="form.keyReport" :placeholder="$t('分类')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item :label="$t('指标Code')" prop="keyCode">
            <el-input v-model="form.keyCode" :placeholder="$t('指标Code')" show-word-limit :maxlength="50" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item :label="$t('指标名称')" prop="keyName">
            <el-input v-model="form.keyName" :placeholder="$t('指标名称')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('公式')" prop="indicatorsFormula">
            <el-autocomplete
              v-model="form.indicatorsFormula"
              class="inline-input"
              :placeholder="$t('公式')"
              readonly
              :trigger-on-focus="false"
            >
              <template slot="append">
                <a @click="selectGS">{{ $t('选择公式') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <!--
        <el-col :span="12">
          <el-form-item :label="$t('重点关注')" prop="focusOn">
            <el-radio-group v-model="form.focusOn" :placeholder="$t('重点关注')">
              <el-radio label="Y">{{ $t('是') }}</el-radio>
              <el-radio label="N">{{ $t('否') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据类型')" prop="keyType">
            <el-radio-group v-model="form.keyType" :placeholder="$t('数据类型')">
              <el-radio label="money">{{ $t('金额') }}</el-radio>
              <el-radio label="ratio">{{ $t('百分比') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        -->
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input v-model="form.sort" :placeholder="$t('排序')" oninput="value=value.replace(/[^\d.]/g,'')" show-word-limit :maxlength="6" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="company:pcw" />
    </div>
    <!-- 添加或修改关键指标公式（系统模板）对话框 -->
    <el-dialog v-el-drag-dialog :title="$t('选择公式')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogPcw" append-to-body>
      <pcwTempKeyFormula ref="pcwTempKeyFormula" :company-id="companyId" :formula="formula" />
    </el-dialog>
  </div>
</template>

<script>
    import { savePcwTempKey, getPcwTempKey } from '@/api/company/pcw/pcwTempKey'
    import pcwTempKeyFormula from '@/views/company/pcw/pcwTempKeyFormula.vue'
    export default {
      components: {
        pcwTempKeyFormula
      },
      props: {
        companyId: {
          type: String,
          required: true,
          default: ''
        }
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  keyReport: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  keyName: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  indicatorsFormula: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  keyCode: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
                },
                // 弹出层标题
                title: '',
                // 是否显示弹出层
                formOpen: false,
                formula: {}
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getPcwTempKey(data.id).then(response => {
                        this.form = response.data
                        // eslint-disable-next-line no-eval
                        this.form.formulaList = eval(this.form.formulaList)
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                companyId: this.companyId,
                typeId: this.$route.query.typeId,
                keyCode: undefined,
                keyName: undefined,
                keyReport: undefined,
                indicatorsFormula: undefined,
                indicatorsCodeFormula: undefined,
                sort: undefined,
                keyType: 'money',
                focusOn: 'N',
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.formulaList = JSON.stringify(this.form.formulaList)
                        savePcwTempKey(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
            /** 选择公式操作 */
            selectGS() {
              this.formOpen = true
              this.formula = {
                name: this.form.indicatorsFormula,
                code: this.form.indicatorsCodeFormula,
                formulaList: this.form.formulaList || []
              }
            },
            addFormula(name, code, formulaList) {
              this.form.indicatorsFormula = name
              this.form.indicatorsCodeFormula = code
              this.form.formulaList = formulaList
            }
        }
    }
</script>
<style scoped>

</style>
